# -*-coding:utf-8 -*-
from bluetools import tools
import sdp_infoleak as sdpi
import bnep_infoleak as bnepi
import ctypes,os,configparser,time
import main.probe
import main as m
so = ctypes.cdll.LoadLibrary

__author__ = 'wuyou'

def leak(mac_list):

    leakinfo = []
    #CVE-2017-0785
    for mac in mac_list:
        aa =sdpi.main(tools.local_blue_dev(),mac)
        if aa != None:
            leakinfo.append(aa)
        bb = bnepi.leak(tools.local_blue_addr(),mac)
        if bb != None:
            leakinfo.append(bb)
        #cc = tools.CVE1782(mac)
        #print cc,type(cc)
       # if cc != None:
           # leakinfo.append(cc)
    # print leakinfo
    return leakinfo
def writeblueleakmac(maclist):
    if type(maclist) == list:
        mac =""
        for i in maclist:
            mac = mac +','+i
        print mac
        os.chdir("/home/wuyou/jctx/ProcessPcap/blueleakScan")
        cf = configparser.ConfigParser()
        cf.add_section('MAC')
        cf.set("MAC", "mac", mac)
        with open("blueleakMAc.ini", "w+") as f:
            cf.write(f)

def getblueleakMac():
    os.chdir("/home/wuyou/jctx/ProcessPcap/blueleakScan")
    cf = configparser.ConfigParser()
    filename = cf.read("blueleakMAc.ini")
    attackmac = cf.get("MAC", "mac")
    mac = attackmac.split(',')[1:]
    print mac
    return mac

if __name__ == '__main__':
    #mac_list =['22:22:82:8e:ca:64']
   # bb = leak(mac_list)
    #print leak(mac_list)
    #mac = ['48:2C:A0:0F:1F:AD','D8:9B:3B:73:8C:EB','22:22:82:8E:CA:64']
    #writeblueleakmac(mac)
   # getblueleakMac()
    while True:
        try:
            time.sleep(10)
            bb = leak(getblueleakMac())
            main.probe.do_log_for_blueleak(bb)
        except:
            pass
   #
    #aa = so('./adder.so')getblueleakMac()
   # print aa.test('22:22:82:8e:ca:64')
    #mylib = so('./bnep_heap_overflow.so')
   # print mylib.test('22:22:82:8e:ca:64')
    #leak(mac_list)